package org.blogwu.learn.multithread.case1;

public class VolatileExample {
    int a = 0;
    boolean flag = false;

    public void writer() {
        a = 1;
        flag = true;
    }

    public void reader() {
        int count = 0;
        while (flag) {
            int i = a;
            System.out.println(count + " " + i);
            if (count > 6) {
                break;
            }
            count++;
        }
    }

    public static void main(String[] args) {
        VolatileExample volatileExample = new VolatileExample();
        // 写
        new Thread(new Runnable() {
            @Override
            public void run() {
                volatileExample.writer();
            }
        }).start();
        // 读
        new Thread(new Runnable() {
            @Override
            public void run() {
                volatileExample.reader();
            }
        }).start();


    }
}
